<?php
require('DBB.php');

class DBControl extends DBB
{
    private static $instance = NULL;

    private function  __construct()
    {
        return NULL;
    }

    /**
     * @return DBControl
     */
    public static function getInstance()
    {
        if (!self::$instance)
        {
            self::$instance = new DBControl();
            self::$DB = DBB::getInstance();

        }
        return self::$instance;
    }

    public function PDOquery($query, $valArray)
    {
        foreach ($valArray as &$val) 
        {
            if ($val === false)
            {
                $val = 'false';
            }
            if ($val === true)
            {
                $val = 'true';
            }
        }

//            echo '<br />query='.$query.'<br />';
//            echo '<pre>';
//            print_r($valArray);
//            echo '</pre><br />';

        $stmt=self::$DB->prepare($query);

        $stmt->execute($valArray);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function insert($tableName, $valArray, $returning=NULL)
    {
//        echo '<pre>';
//        print_r($valArray);
//        echo '</pre><br />';
        $cols = implode(", ", array_keys($valArray));
        $vals = array_values($valArray);
        $qry = 'INSERT INTO '. $tableName . ' ('. $cols .')' . ' VALUES (';

        $i=0;
        foreach($vals as $val)
        {
            //dopisywanie do stringa
            if ($i != count($vals)-1)
                $qry = $qry . '?, ';
            else
                $qry = $qry . '?)';
            $i++;
        }

        if ($returning != NULL)
        {
            $qry = $qry . " RETURNING " . $returning;
        }
//        echo 'query='.$qry;
        return $this->PDOquery($qry, $vals);
    }

    public function update($tableName, $newValArray, $selectorArray)
    {
        //UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value
//        $db -> update ('users', array('height' => 170), array('id' => 10))
        $newValCols = array_keys($newValArray);
        $newValVal = array_values($newValArray);
        $selectorCols = array_keys($selectorArray);
        $selectorVal = array_values($selectorArray);

        $qry = 'UPDATE ' . $tableName . ' SET ';

        $i=0;
        foreach($newValCols as $val)
        {
            //dopisywanie zapytajnikow i kolumn do stringa
            if ($i != count($newValCols)-1)
                $qry = $qry . $val . '=?, ';
            else
                $qry = $qry . $val . '=?';
            $i++;
        }

        $qry = $qry . ' WHERE ';

        $i=0;
        foreach($selectorCols as $col)
        {
            //dopisywanie zapytajnikow i kolumn do stringa
            if ($i != count($selectorCols)-1)
                $qry = $qry . $col . '=? AND ';
            else
                $qry = $qry . $col . '=?';
            $i++;
        }

//        echo $qry;
        $this->PDOquery($qry, array_merge($newValVal, $selectorVal));
    }
    
    public function select($tableName, $conditions=NULL, $colsArray=array('*'), $orderBy=NULL)
    {
        //SELECT col1, col2... FROM tablename WHERE a=x ORDER BY col1 ASC, col2 DESC LIMIT 5
        //SELECT id, data FROM olaboga WHERE id=10 AND data=15 ORDER BY id, data ASC, data DESC
        //select('olaboga', array('id' => 10, data => 15), array('id', 'data'), array('id' => 'ASC', 'data' => 'DESC'))
        $qry = 'SELECT ' . implode(", ", $colsArray) . ' FROM ' . $tableName;
        $vals = array();
        if ($conditions!=NULL)
        {
            $qry = $qry . ' WHERE ';

            //dodawanie warunkow do WHERE
            $i=0;
            foreach($conditions as $condKey => $condVal)
            {
                if ($i != count($conditions)-1)
                    $qry = $qry . $condKey . '=? AND ';
                else
                    $qry = $qry . $condKey . '=? ';
                $i++;
            }

            //dopisanie wartosci do tablicy przekazywanej
            $vals = array_values($conditions);
        }

        if ($orderBy != NULL)
        {
            //dodawanie warunkow do ORDER BY
            $qry = $qry . ' ORDER BY ';
            $i=0;
            foreach($orderBy as $orderKey => $orderVal)
            {
                if ($i != count($orderBy)-1)
                    $qry = $qry . $orderKey . ' ' . $orderVal . ', ';
                else
                    $qry = $qry . $orderKey . ' ' . $orderVal . ' ';
                $i++;
            }
        }
        
        return $this->PDOquery($qry, $vals);
    }

    public function delete($tableName, $selectorArray/*, $returning=NULL*/)
    {
        //DELETE FROM OLABOGA WHERE ID = 10
        $qry = 'DELETE FROM ' . $tableName . ' WHERE ';

        $i=0;
        foreach($selectorArray as $selectorKey => $selectorVal)
        {
            if ($i != count($selectorArray)-1)
                $qry = $qry . $selectorKey . '=? AND ';
            else
                $qry = $qry . $selectorKey . '=?';
            $i++;
        }
        $vals = array_values($selectorArray);
//        if ($returning != NULL)
//        {
//            $qry = $qry . " RETURNING " . $returning;
//        }
        $this->PDOquery($qry, $vals);
    }
}
?>